#include <QApplication>
#include <QMainWindow>
#include <QAction>
#include <QAction>
#include <QMenuBar>
#include <QWidget>
#include <QToolBar>
#include <QPlainTextEdit>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QStatusBar>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDesktopWidget>
#include <QCheckBox>
#include <QListWidget>
#include <QStackedWidget>
#include <QDir>
#include <QFont>
#include <QIcon>
#include <QStringList>
#include <QMessageBox>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <QProcess>
#include <QDebug>
//for xml parse (must add "Qt += xml" to .pro file)
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QDomNodeList>
//for stardard c++
#include <iostream>
using namespace std;

#include "bbcpodcast.h"

bbcpodcast::bbcpodcast(QWidget *parent)
  : QWidget(parent)
{
  setStyleSheet("QPushButton{width:200px;height:60px;font: bold normal 24pt 'Courier New'}");
  QPushButton *restartBtn = new QPushButton(tr("Restart"));
  QPushButton *nextBtn = new QPushButton(tr("Next"));
  QPushButton *hideBtn = new QPushButton(tr("Hide"));
  QPushButton *quitBtn = new QPushButton(tr("Quit"));
  
  connect(restartBtn, SIGNAL(clicked()), this, SLOT(getPodcast()));
  connect(nextBtn, SIGNAL(clicked()), this, SLOT(directToNext()));
  connect(hideBtn, SIGNAL(clicked()), this, SLOT(hide()));
  connect(quitBtn, SIGNAL(clicked()), qApp, SLOT(quit()));
  statusLabel = new QLabel();

  QLabel *blankLabel = new QLabel();		//just for eee note
  
  QVBoxLayout *layout= new QVBoxLayout;
  layout->addWidget(blankLabel);		//just for eee note
  layout->addWidget(restartBtn);
  layout->addWidget(nextBtn);
  layout->addWidget(hideBtn);
  layout->addWidget(quitBtn);
  layout->addWidget(statusLabel);
  layout->addStretch();
  setLayout(layout);

  //mplayer = NULL;	//initilization
  mplayer = new QProcess(this);
  connect(mplayer, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(playNext()));
  
  getPodcast();		//start
}

void bbcpodcast::getPodcast()
{
  if(mplayer->state()!=QProcess::NotRunning)
	{
	  //now running, first stop it
	  linkIndex = -1;	//not continue playing
	  mplayer->kill();		//stop it will cause to invoke slot function playNext()
	}
  statusLabel->setText("getting rss ...");
  QString rssurl = "http://downloads.bbc.co.uk/podcasts/worldservice/globalnews/rss.xml";
  QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
  manager->get(QNetworkRequest(QUrl(rssurl)));	//send request
}

/*
 * this function should not blocked !
 *
void bbcpodcast::replyFinished(QNetworkReply *reply)
{
  qDebug() << "reply finished ..." << endl;

  QDomDocument doc("mydocument");
  doc.setContent(reply->readAll());
  
  //doc.childNodes().item(1)	//<rss> (the 0 is <xml>)
  //doc.childNodes().item(1).firstChild()	//<channel>
  QDomNodeList list = doc.childNodes().item(1).firstChild().childNodes();
  int count = 0;
  for(int i=0;i<list.length();i++)
	{
	  QDomNode node = list.item(i);
	  if(node.nodeName()=="item")
		{
		  count++;
		  QString title = node.firstChildElement("title").text();
		  QString link = node.firstChildElement("link").text();

		  QStringList arg;
		  arg << link;
		  QProcess *mplayer = new QProcess(this);
		  qDebug() << "now playing " << link;
		  mplayer->start("mplayer", arg);

		  mplayer->waitForFinished(-1);		//wait until finished
		  
		  //qDebug() << title << endl;
		  //qDebug() << link << endl;
		  
		}
	}
  qDebug() << "total " << count << " items";
  //  close();
}
*/

void bbcpodcast::replyFinished(QNetworkReply *reply)
{
  QDomDocument doc("mydocument");
  doc.setContent(reply->readAll());
  //doc.childNodes().item(1)	//<rss> (the 0 is <xml>)
  //doc.childNodes().item(1).firstChild()	//<channel>
  QDomNodeList list = doc.childNodes().item(1).firstChild().childNodes();
  podcastLinks.clear();
  for(int i=0;i<list.length();i++)
	{
	  QDomNode node = list.item(i);
	  if(node.nodeName()=="item")
		{
		  QString title = node.firstChildElement("title").text();
		  QString link = node.firstChildElement("link").text();
		  podcastLinks << link;
		  podcastTitles << title;
		  //qDebug() << title << endl;
		  //qDebug() << link << endl;
		}
	}
  qDebug() << "total " << podcastLinks.length() << " items";

  linkIndex = 0;
  if(podcastLinks.length() == 0)
	statusLabel->setText("Can't get podcast, network may not connected!");
  else
	{
	  statusLabel->setText("total get "+QString::number(podcastLinks.length())+" items");
	  playNext();			//play
	}
}

void bbcpodcast::playNext()
{
  if(linkIndex < 0)	return;	//cooperate to force stop playing
  if(linkIndex >= podcastLinks.length())
	{
	  statusLabel->setText("play finished");
	  return;
	}
  QString title = podcastTitles.at(linkIndex);
  QString link = podcastLinks.at(linkIndex++);
  QStringList arg;
  arg << link;
  //mplayer = new QProcess(this);
  //connect(mplayer, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(playNext()));
  qDebug() << "now playing " << link;
  mplayer->start("mplayer", arg);

  statusLabel->setText(title);
  
  //mplayer->waitForFinished(-1);		//wait until finished
}

void bbcpodcast::directToNext()
{
  if(mplayer->state()!=QProcess::NotRunning)
	{
	  //now running, stop it will cause to invoke slot function playNext()
	  mplayer->kill();
	}
  else
	{
	  playNext();
	}
}


